package com.byzk.p2p.admin.system.service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.base.util.SMSUtil;
import com.byzk.p2p.admin.system.dao.SmsRecordDao;
import com.byzk.p2p.admin.system.dto.SMSRecord;
import com.byzk.p2p.admin.system.vo.SmsRecordVo;
import com.byzk.p2p.admin.user.dao.UserAccountDao;
import com.byzk.p2p.admin.user.dto.UserAccount;

@Service
public class SMSService {
	@Autowired
	private UserAccountDao accountDao;
	@Autowired
	private SmsRecordDao smsRecordDao;
	
	static SMSUtil util=new SMSUtil();
	
	public int addSmsRecord(SMSRecord record){
		return smsRecordDao.insert(record);
	}
	
	/**
	 * 1、注册验证码短信提示
	 * @throws IOException 
	 */
	public void sendCaptchaCodeSms(String mobile, String code,String ip) throws IOException{
		String content="您的本次验证码："+code+"，感谢使用迅泊达。请勿泄漏给他人。";
		util.registerSMS(mobile, code);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setPhone(mobile);
		record.setContent(content);
		record.setSendIp(ip);
		record.setRemark("注册短信");
		record.setUserId(-1);
		addSmsRecord(record);
	}

	
	/**
	 * 2、充值短信提醒
	 */
	public void sendRechargeSms(String mobile,double money,Integer uid) throws IOException{
		UserAccount account=accountDao.getByUserId(uid);
		double amonut=account.getBalance();
		String content="您的账户已成功充值"+money+"元，当前账户余额"+amonut+"元，请登录系统在我的账户中查询。";
		
//		util.sendSMS(mobile, content);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setPhone(mobile);
		record.setContent(content);
		record.setRemark("充值短信");
		record.setUserId(uid);
		addSmsRecord(record);
	}
	
	
	/**
	 * 3、找回密码
	 */
	public void sendForgetPwdSms(String mobile, String code) throws IOException{
		String content="验证码："+code+"，您正在使用登录密码找回功能，请勿向任何人提供验证码信息。";
		
		util.registerSMS(mobile, code);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setPhone(mobile);
		record.setContent(content);
		record.setRemark("找回密码短信");
		record.setUserId(-1);
		addSmsRecord(record);
	}
	
	/**
	 * 3、找回交易密码
	 */
	public void sendForgetTransactionPwdSms(String mobile, String code) throws IOException{
		String content="验证码："+code+"，您正在使用交易密码找回功能，请勿向任何人提供验证码信息。";
		
		util.registerSMS(mobile, code);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setPhone(mobile);
		record.setContent(content);
		record.setRemark("找回交易密码短信");
		record.setUserId(-1);
		addSmsRecord(record);
	}
	
	/**
	 * 3、找回密码(PC端)
	 */
	public void sendForgetPwdSms(String mobile, String code, String ip) throws IOException{
		String content="验证码："+code+"，您正在使用登录密码找回功能，请勿向任何人提供验证码信息。";
		
		util.registerSMS(mobile, code);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setSendIp(ip);
		record.setPhone(mobile);
		record.setContent(content);
		record.setRemark("找回密码短信");
		record.setUserId(-1);
		addSmsRecord(record);
	}
	
	/**
	 * 4、提现
	 */
	public void sendWithdrawSms(String mobile,double money,Integer uid) throws IOException{
		UserAccount account=accountDao.getByUserId(uid);
		String content="您已从账号成功提取"+money+"元，当前账户余额"+account.getBalance()+"元，系统将于1-3个工作日内处理到账。近期请留意您的银行卡是否到账。";
		
//		util.sendSMS(mobile, content);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setPhone(mobile);
		record.setContent(content);
		record.setRemark("提现短信");
		record.setUserId(uid);
		addSmsRecord(record);

	}
	
	/**
	 * 换绑手机号--发旧手机号
	 * @param mobile
	 * @param code
	 * @param ip
	 */
	public void sendOldCodeSms(String mobile, String code,String ip) throws IOException{
		String content="原手机号验证码为："+code+"，您正在使用换绑手机号功能。";
		
		util.registerSMS(mobile, code);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setPhone(mobile);
		record.setContent(content);
		record.setSendIp(ip);
		record.setRemark("换绑手机号短信");
		record.setUserId(-1);
		addSmsRecord(record);
	}

	
	/**
	 * 换绑手机号--发旧手机号
	 * @param mobile
	 * @param code
	 * @param ip
	 */
	public void sendNewCodeSms(String mobile, String code,String ip) throws IOException{
		String content="新手机号验证码为："+code+"，您正在使用换绑手机号功能。";
		
		util.registerSMS(mobile, code);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setPhone(mobile);
		record.setContent(content);
		record.setSendIp(ip);
		record.setRemark("换绑手机号短信");
		record.setUserId(-1);
		addSmsRecord(record);
	}

	/**
	 * 提现验证码
	 * @param mobile
	 * @param code
	 * @param ip
	 */
	public void sendwithdrawApplySms(String mobile, String code,String ip) throws IOException{
		String content="验证码为："+code+"，您正在使用提现功能。";
		
		util.registerSMS(mobile, code);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setPhone(mobile);
		record.setContent(content);
		record.setSendIp(ip);
		record.setRemark("换绑手机号短信");
		record.setUserId(-1);
		addSmsRecord(record);
	}
	
	/**
	 * 还款短信
	 */
	public void sendPaymentSms(String mobile,double money,Integer uid) throws IOException{
		String content="您有一笔投资已还款，金额为："+money+"元"+" ，快去您的帐户查收并继续投资赚钱吧！";
		
		//TODO 还款短信测试阶段屏蔽
		//util.sendSMS(mobile, content);
		
		SMSRecord record=new SMSRecord();
		record.setCreateTime(new Date());
		record.setPhone(mobile);
		record.setContent(content);
		record.setRemark("还款短信");
		record.setUserId(uid);
		addSmsRecord(record);

	}
	
	public PageInfo<SmsRecordVo> getSmsRecordList(Map<String, Object> queryMap,Integer index,Integer limit){
		if (index != null && limit != null) {
			if (queryMap == null) {
				queryMap = new HashMap<String, Object>();
			}
			queryMap.put("start", (index-1)*limit);
			queryMap.put("limit", limit);
		}
		int total = smsRecordDao.count(queryMap);
		List<SmsRecordVo> list = smsRecordDao.list(queryMap);
		PageInfo<SmsRecordVo> pageInfo = new PageInfo<SmsRecordVo>(total, index, limit);
		pageInfo.setPageList(list);
		return pageInfo;
	}
	
	/**
	 * 获取当天一个ip的验证码获取次数
	 * @param sendIp 请求方的ip
	 * @param remark 备注为'找回密码短信'或'注册短信'
	 * @return
	 */
	public int countSendIp(String sendIp,String remark) {
		return smsRecordDao.countSendIp(sendIp,remark);
	}
	
	/**
	 * 获取当天一个手机号的验证码获取次数
	 * @param phone 请求方的手机号
	 * @param remark 备注为'找回密码短信'或'注册短信'
	 * @return
	 */
	public int countSendPhone(String phone,String remark) {
		return smsRecordDao.countSendPhone(phone,remark);
	}
}